

package com.l2qq.vben.auth;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.l2qq.admin.login.LoginService;
import com.l2qq.common.kit.CommonKit;
import com.l2qq.common.kit.IpKit;


import java.util.ArrayList;
import java.util.List;


/**
 * 登录控制层
 */
@Path(value = "/auth")
public class AuthController extends Controller {

    @Inject
    LoginService loginService;

    /**
     * 登录
     */
    @Clear
    public void login() {
        // 系统关闭检查（保持原逻辑）
        CommonKit.setIsCloseSystem(Db.queryInt("SELECT count(1) FROM dict WHERE k = 'closeSystem' AND v = 'true' ") > 0);
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        String user_name = jsonObject.getString("username");
        String pass_word = jsonObject.getString("password");
        String ip = IpKit.getRealIp(getRequest(), "127.0.0.1");
        int port = getRequest().getServerPort();
        Ret ret = loginService.login(user_name, pass_word, ip, port);
        renderJson(ret);
    }

    public void codes(){
        Ret ret = Ret.ok();
        List<String> list = new ArrayList<>();
        //list.add("AC_1000001");
        //list.add("AC_1000002");
        renderJson(ret.set("data",list));
    }

    /**
     * 退出登录
     */
    @Clear
    public void logout() {
        // Sa-Token 登出
        StpUtil.logout();
        renderJson(Ret.ok());
    }

}


